package com.example.hetong_system.controller;

import com.example.hetong_system.model.Employee;
import com.example.hetong_system.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;

    // 修改密码接口
    @PutMapping("/employee/change-password")
    public ResponseEntity<?> changeEmployeePassword(
            @RequestParam String name,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {

        System.out.println("收到修改密码请求：" + name + ", " + oldPassword);

        // 查找员工：根据 name 和 oldPassword 验证
        return employeeRepository.findByNameAndPassword(name, oldPassword)
                .map(employee -> {
                    // 更新密码
                    employee.setPassword(newPassword);
                    employeeRepository.save(employee);
                    return ResponseEntity.ok("密码修改成功");
                })
                .orElseGet(() -> ResponseEntity.badRequest().body("用户名或原密码错误"));
    }
}